package com.jokerpan.project.controller;

import com.jokerpan.project.annotation.AccessLimit;
import com.jokerpan.project.annotation.LogAction;
import com.jokerpan.project.commons.ActionResult;
import com.jokerpan.project.commons.ListResult;
import com.jokerpan.project.commons.ResultBuilder;
import com.jokerpan.project.entity.BlogAction;
import com.jokerpan.project.service.BlogActionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("blogAction")
@Api("博客、评论操作Api")
public class BlogActionController {

    @Resource
    BlogActionService blogActionService;

    @GetMapping("/queryList")
    @AccessLimit(seconds = 6, maxCount = 5)
    @ApiOperation("查询所有数据分页")
    public ListResult<Object> queryList(BlogAction blogAction) {
        return ResultBuilder.buildListSuccess(this.blogActionService.queryList(blogAction));
    }

    @GetMapping("/queryChartData")
    @AccessLimit(seconds = 6, maxCount = 5)
    @ApiOperation("查询趋势图数据")
    public ListResult<Object> queryChartData(BlogAction blogAction) {
        return ResultBuilder.buildListSuccess(this.blogActionService.queryChartData(blogAction));
    }

    @PostMapping("/add")
    @ApiOperation("新增操作")
    @AccessLimit(seconds = 6, maxCount = 5)
    @PreAuthorize("hasPermission('','user:update')")
    public ActionResult add(@RequestBody BlogAction blogAction){
        if (this.blogActionService.add(blogAction)){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed();
        }
    }

    @GetMapping("/delete")
    @ApiOperation("删除操作")
    @AccessLimit(seconds = 6, maxCount = 5)
    @PreAuthorize("hasPermission('','user:update')")
    @LogAction("博客、评论操作管理,删除博客、评论操作数据,删除数据,删除")
    public ActionResult delete(@RequestParam("id") Integer id){
        return this.blogActionService.deleteById(id);
    }
}
